package wifiunion.com.notice.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;


import java.util.ArrayList;

import wifiunion.com.notice.CommonUtils;
import wifiunion.com.notice.DeviceDialogCallBack;
import wifiunion.com.notice.R;
import wifiunion.com.notice.RecyclerItemClick;
import wifiunion.com.notice.adapter.DeviceSelectSthAdapter;
import wifiunion.com.notice.entity.DeviceSelectSth;

/**
 * Created by Administrator on 2016/12/5.
 * 设备选择相关的对话框
 */

public class DeviceSelectSthDialog extends Dialog implements RecyclerItemClick {
    private Context context;
    private TextView titleName;

    private DeviceSelectSthAdapter mAdapter;

    private ArrayList<DeviceSelectSth> mList = new ArrayList<DeviceSelectSth>();

    private TextView cancelTxt;
    private TextView confirmTxt;


    private RecyclerView mRecyclerView;


    private DeviceDialogCallBack mCallback;

    private boolean isSingleSelect = false;
    /**
     * 是否有全部这个选项 涉及到选择全部选项的时候，其他子项被取消选择
     */
    private boolean hasAllItem = true;


    public DeviceSelectSthDialog(Context context, ArrayList<DeviceSelectSth> datalist, DeviceDialogCallBack callback) {
        super(context, R.style.alert_dialog);
        this.context = context;

        mList = datalist;
        mCallback = callback;

        LayoutInflater li = LayoutInflater.from(context);
        ViewGroup contentView = (ViewGroup) li.inflate(
                R.layout.dialog_deviceselect, null);
        contentView.bringToFront();


        cancelTxt = (TextView) contentView.findViewById(R.id.cancel_txt);
        confirmTxt = (TextView) contentView.findViewById(R.id.confirm_txt);
        titleName = (TextView) contentView.findViewById(R.id.tv_title_name);
        cancelTxt = (TextView) contentView.findViewById(R.id.cancel_txt);

        mRecyclerView = (RecyclerView) contentView.findViewById(R.id.deviceselect_recyclerview);


        mAdapter = new DeviceSelectSthAdapter(getContext(), mList, this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(context));

        mRecyclerView.setAdapter(mAdapter);


        mAdapter.setOnclickListener(this);


        setCancelListener();
        setComfirmListener();

        setCanceledOnTouchOutside(true);

        setContentView(contentView);


    }


    public void setIsSingleSelect(boolean flag) {
        isSingleSelect = flag;
    }

    public void setHasAllItem(boolean flag) {
        hasAllItem = flag;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = getContext().getResources().getDimensionPixelSize(
                R.dimen.dp_480);
        lp.height = (int) (CommonUtils.getScreenHeight(getContext()) * 0.9f);
        onWindowAttributesChanged(lp);
    }

    public void setTitleMsg(String msg) {
        if (titleName != null) {
            titleName.setText(msg);
        }
    }

    public void setCancleText(String text) {
        if (text != null) {
            cancelTxt.setText(text);
        }
    }


    public void setComfirmListener() {
        if (confirmTxt != null) {
            confirmTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCallback.updateList(mList);
                    dismiss();
                }

            });
        }
    }

    public void setCancelListener() {
        if (cancelTxt != null) {
            cancelTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        }
    }


    @Override
    public void onItemClick(View view, int postion) {
        if (isSingleSelect) {
            for (int i = 0; i < mList.size(); i++) {
                if (postion == i) {
                    mList.get(i).setSelected(true);
                } else {
                    mList.get(i).setSelected(false);
                }
            }
        } else {

            if (mList.get(postion).isSelected()) {
                mList.get(postion).setSelected(false);
            } else {
                // 不限 项被选中 其他项 取消选中
                if (postion == 0) {
                    if (hasAllItem) {
                        for (int i = 0; i < mList.size(); i++) {
                            if (postion != i) {
                                mList.get(i).setSelected(false);
                            }
                        }
                    }
                } else {
                    if (hasAllItem) {
                        //非 不限 项被选中  不限项取消选中
                        if (mList.get(0).isSelected()) {
                            mList.get(0).setSelected(false);
                        }
                    }
                }
                mList.get(postion).setSelected(true);
            }
        }
        mAdapter.notifyDataSetChanged();
    }
}
